package com.auto.autoapp.util

import android.view.accessibility.AccessibilityNodeInfo

const val success = 0
const val nodeNotFind = 1
const val clickFail = 2
const val tapFail = 3
const val scrollFail = 4
const val setInputFail = 5
const val notLogin = 6
const val noComment = 7
const val jumpOp = 8
const val appNotOpen = 9
val codeMsgMap =
    mapOf(
        success to "",
        nodeNotFind to "",
        clickFail to "",
        tapFail to "",
        scrollFail to "",
        notLogin to ""
    )

class CustomException(
    val code: Int,
    message: String? = null,
    cause: Throwable? = null
) : Throwable(message ?: getDefaultMessage(code), cause) {
    companion object {
        private fun getDefaultMessage(code: Int): String {
            return codeMsgMap.getOrDefault(code, "未知错误 (code: $code)")

        }
    }

    // 可选：重写 toString 方法以包含 code 信息
    override fun toString(): String {
        return "${this::class.simpleName}(code=$code): $message"
    }
}

fun Int.nodeCodeException(): Result<AccessibilityNodeInfo> {
    return Result.failure(CustomException(this))
}

fun Int.opException(): Result<Int> {
    return Result.failure(CustomException(this))
}